home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Pascal / Utilities / MandelNet / mandelbrot.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-14  |  12.5 KB  |  590 lines  |  [TEXT/KAHL]

  1. #include "mandel.h"
  2.  
  3.  
  4.  
  5. AssignMandelbrot()
  6. {
  7. int                i,errCode,numRows,responseCode,currentRow;
  8. char            tempString[256];
  9. int                theRow[maxSlaves];
  10. long            finalTicks[maxSlaves];
  11.  
  12.     SetCursor(&waitCursor);
  13.     
  14.     if (slaveMode==TRUE || numSlaves<=0)
  15.     {
  16.         DisplayMessage("Generating Mandelbrot Image...");
  17.         
  18.         Mandelbrot(1,254);
  19.         
  20.         DisplayMessage("Completed Mandelbrot Image");
  21.     }
  22.         
  23.     else
  24.     {
  25.         numRows = graphRect.bottom - graphRect.top - 2;
  26.         numRows = 0.5 + (double) numRows/(double) pixelSize;
  27.         
  28.         currentRow = graphRect.top + 1;
  29.         
  30.         for (i=0;i<numSlaves;i++)
  31.         {
  32.             if (BreakKey())
  33.                 goto EXITPOINT;
  34.             
  35.             errCode = SendWorkOrder(i,currentRow);
  36.             
  37.             if (errCode != noErr)
  38.                 slaveStatus[i] = FALSE;
  39.             else
  40.             {
  41.                 slaveStatus[i] = TRUE;
  42.                 theRow[i] = currentRow;
  43.                 currentRow += pixelSize;
  44.                 finalTicks[i] = TickCount();
  45.             }
  46.         }
  47.         
  48.         
  49.         while (numRows)
  50.         {
  51.             if (meToo && currentRow<graphRect.bottom-1)
  52.             {
  53.                 if (Mandelbrot(currentRow,currentRow+pixelSize) != noErr)
  54.                     goto EXITPOINT;
  55.                 else
  56.                 {
  57.                     CopyBits(&(theWPtr->portBits),&graphBits,&graphRect,&graphRect,srcCopy,0L);
  58.                     currentRow += pixelSize;
  59.                     numRows--;
  60.                 }
  61.             }
  62.         
  63.             for (i=0;numRows && i<numSlaves;i++)
  64.             {
  65.                 if (BreakKey())
  66.                     goto EXITPOINT;
  67.                 
  68.                 if (slaveStatus[i]==TRUE && TickCount()>=finalTicks[i])
  69.                 {
  70.                     errCode = QueryNode(i,&responseCode);
  71.                     
  72.                     if (errCode == noErr)
  73.                     {
  74.                         if (responseCode==busyResponse)
  75.                         {
  76.                             sprintf(tempString,"Node #%d still busy",slaveNode[i]);
  77.                             DisplayMessage(tempString);
  78.                             
  79.                             finalTicks[i] = TickCount() + 60.0*timeDelay;
  80.                         }
  81.                         
  82.                         else if (responseCode==idleResponse)
  83.                         {
  84.                             if (theRow[i] > 0)
  85.                             {
  86.                                 errCode = GetBitMap(i,theRow[i]);
  87.                                 
  88.                                 if (errCode == noErr)
  89.                                 {
  90.                                     theRow[i] = -1;
  91.                                     numRows--;
  92.                                 }
  93.                                 
  94.                                 finalTicks[i] = TickCount();
  95.                             }
  96.                             
  97.                             if (theRow[i]<0 && currentRow<graphRect.bottom-1)
  98.                             {
  99.                                 errCode = SendWorkOrder(i,currentRow);
  100.             
  101.                                 if (errCode != noErr)
  102.                                     slaveStatus[i] = FALSE;
  103.                                 
  104.                                 else
  105.                                 {
  106.                                     slaveStatus[i] = TRUE;
  107.                                     theRow[i] = currentRow;
  108.                                     currentRow += pixelSize;
  109.                                     finalTicks[i] = TickCount();
  110.                                 }
  111.                             }
  112.                         }
  113.                     }
  114.                 }
  115.             }
  116.         }
  117.     }
  118.     
  119. EXITPOINT:
  120.  
  121.     InitCursor();
  122. }
  123.  
  124.  
  125.  
  126.  
  127.  
  128. Mandelbrot(firstRow,lastRow)
  129. int        firstRow,lastRow;
  130. {
  131. int                errCode,i,j,iterations;
  132. double            cx,cy,x,y,tx,ty,r;
  133. GrafPtr            oldPort;
  134. EventRecord        theEvent;
  135. char            theChar;
  136. Rect            theRect,myClipRect;
  137.  
  138.     errCode = noErr;
  139.     
  140.     GetPort(&oldPort);
  141.     SetPort(theWPtr);
  142.     
  143.     myClipRect = graphRect;
  144.     InsetRect(&myClipRect,1,1);
  145.     ClipRect(&myClipRect);
  146.     
  147.     PenNormal();
  148.     
  149.     
  150.     
  151.     for (i=firstRow;i<lastRow;i+=pixelSize)
  152.     {
  153.         for (j=1;j<255;j+=pixelSize)
  154.         {
  155.             theRect.left = j + graphRect.left;
  156.             theRect.top = i + graphRect.top;
  157.             theRect.right = theRect.left + pixelSize;
  158.             theRect.bottom = theRect.top + pixelSize;
  159.             
  160.             PenPat(black);
  161.             PenMode(patXor);
  162.             PaintRect(&theRect);
  163.             
  164.             cx = XToD(graphRect.left + j + pixelSize/2);
  165.             cy = YToD(graphRect.top + i + pixelSize/2);
  166.             
  167.             x = cx;
  168.             y = cy;
  169.             
  170.             iterations = 0;
  171.             
  172.             do
  173.             {
  174.                 tx = x*x-y*y + cx;
  175.                 ty = 2.0*x*y + cy;
  176.                 
  177.                 r = tx*tx + ty*ty;
  178.                 
  179.                 iterations++;
  180.                 
  181.                 x = tx;
  182.                 y = ty;
  183.                 
  184.             } while (r<4.0 && iterations<maxIterations);
  185.             
  186.             if (r >= 4.0)
  187.             {
  188.                 if (shading)
  189.                 {
  190.                     if (iterations < maxIterations/4)
  191.                         PenPat(white);
  192.                     else if (iterations < 2*maxIterations/4)
  193.                         PenPat(ltGray);
  194.                     else if (iterations < 3*maxIterations/4)
  195.                         PenPat(gray);
  196.                     else if (iterations < maxIterations)
  197.                         PenPat(dkGray);
  198.                     else
  199.                         PenPat(black);
  200.                 }
  201.                 else
  202.                     PenPat(white);
  203.             }
  204.             else
  205.                 PenPat(black);
  206.                 
  207.             PenMode(patCopy);
  208.             PaintRect(&theRect);
  209.             
  210.             
  211.             
  212.             if (BreakKey())
  213.             {
  214.                 errCode = -1;
  215.                 goto EXITPOINT;
  216.             }
  217.             
  218.             if (slaveMode == TRUE)
  219.             {
  220.                 if (GetNextEvent(networkMask,&theEvent))
  221.                 {
  222.                     if ((**receiveRecord).atpProto.atpUserData == stopOrder)
  223.                     {
  224.                         DisplayMessage("Confirming stopOrder");
  225.             
  226.                         (**receiveRecord).atpProto.atpRspUData = confirmOrder;
  227.                         
  228.                         (**receiveRecord).atpProto.atpSocket = mySocket;
  229.                         (**receiveRecord).atpProto.atpRspBuf = 0L;
  230.                         (**receiveRecord).atpProto.atpRspSize = 0;
  231.                 
  232.                         errCode = ATPResponse(receiveRecord,FALSE);
  233.                         
  234.                         if (errCode != noErr)
  235.                             ErrorMessage("ATPResponse",errCode);
  236.                         
  237.                         errCode = -1;
  238.                         
  239.                         goto EXITPOINT;
  240.                     }
  241.                     
  242.                     else if ((**receiveRecord).atpProto.atpUserData == queryStatus)
  243.                     {
  244.                         (**receiveRecord).atpProto.atpRspUData = busyResponse;
  245.                         
  246.                         (**receiveRecord).atpProto.atpSocket = mySocket;
  247.                         (**receiveRecord).atpProto.atpRspBuf = 0L;
  248.                         (**receiveRecord).atpProto.atpRspSize = 0;
  249.                 
  250.                         errCode = ATPResponse(receiveRecord,FALSE);
  251.                         
  252.                         if (errCode != noErr)
  253.                             ErrorMessage("ATPResponse",errCode);
  254.                         
  255.                         AwaitRequest(receiveRecord,&theMessage);
  256.                     }
  257.                 }
  258.             }
  259.         }
  260.     }
  261.  
  262. EXITPOINT:
  263.  
  264.     ClipRect(&nullClipRect);
  265.     SetPort(oldPort);
  266.     
  267.     return(errCode);
  268. }
  269.  
  270.  
  271.  
  272. StopSlaves()
  273. {
  274. int                i,errCode;
  275. char            tempString[256];
  276. ABRecHandle        sendRecord;
  277.  
  278.  
  279.     SetCursor(&waitCursor);
  280.     
  281.     if (numSlaves > 0)
  282.     {
  283.         sendRecord = (ABRecHandle) NewHandle(atpSize);
  284.         
  285.         
  286.         for (i=0;i<numSlaves;i++)
  287.         {
  288.             sprintf(tempString,"Sending stopOrder to node #%d",slaveNode[i]);
  289.             DisplayMessage(tempString);
  290.             
  291.             
  292.             (**sendRecord).atpProto.atpAddress.aNet = slaveNet[i];
  293.             (**sendRecord).atpProto.atpAddress.aNode = slaveNode[i];
  294.             (**sendRecord).atpProto.atpAddress.aSocket = slaveSocket[i];
  295.             
  296.             (**sendRecord).atpProto.atpReqCount =  0;
  297.             (**sendRecord).atpProto.atpDataPtr = 0L;
  298.             
  299.             (**sendRecord).atpProto.atpXO = FALSE;
  300.             (**sendRecord).atpProto.atpTimeOut = rtInterval;
  301.             (**sendRecord).atpProto.atpRetries = rtCount;
  302.             
  303.             (**sendRecord).atpProto.atpRspBuf = 0L;
  304.             (**sendRecord).atpProto.atpRspSize =  0;
  305.             
  306.             (**sendRecord).atpProto.atpUserData = stopOrder;
  307.             
  308.             errCode = ATPRequest(sendRecord,FALSE);
  309.             
  310.             if (errCode || (**sendRecord).atpProto.atpRspUData!=confirmOrder)
  311.             {
  312.                 sprintf(tempString,"Node #%d doesn't respond to stop order",slaveNode[i]);
  313.                 DisplayMessage(tempString);
  314.                 
  315.                 slaveStatus[i] = FALSE;
  316.             }
  317.             
  318.             else
  319.             {
  320.                 sprintf(tempString,"stopOrder confirmed by node #%d",slaveNode[i]);
  321.                 DisplayMessage(tempString);
  322.                 
  323.                 slaveStatus[i] = TRUE;
  324.             }
  325.         }
  326.     
  327.         DisposHandle(sendRecord);
  328.     }
  329.     
  330.     InitCursor();
  331. }
  332.  
  333.  
  334.  
  335. CheckSlaves()
  336. {
  337. int                i,errCode,busy,idle;
  338. char            tempString[256];
  339. ABRecHandle        sendRecord;
  340.  
  341.     SetCursor(&waitCursor);
  342.     
  343.     LookupSlaves();
  344.     DrawInfo();
  345.     
  346.     if (numSlaves > 0)
  347.     {
  348.         sendRecord = (ABRecHandle) NewHandle(atpSize);
  349.         busy = 0;
  350.         idle = 0;
  351.         
  352.         for (i=0;i<numSlaves;i++)
  353.         {
  354.             sprintf(tempString,"Checking node #%d",slaveNode[i]);
  355.             DisplayMessage(tempString);
  356.             
  357.             
  358.             (**sendRecord).atpProto.atpAddress.aNet = slaveNet[i];
  359.             (**sendRecord).atpProto.atpAddress.aNode = slaveNode[i];
  360.             (**sendRecord).atpProto.atpAddress.aSocket = slaveSocket[i];
  361.             
  362.             (**sendRecord).atpProto.atpReqCount =  0;
  363.             (**sendRecord).atpProto.atpDataPtr = 0L;
  364.             
  365.             (**sendRecord).atpProto.atpXO = FALSE;
  366.             (**sendRecord).atpProto.atpTimeOut = rtInterval;
  367.             (**sendRecord).atpProto.atpRetries = rtCount;
  368.             
  369.             (**sendRecord).atpProto.atpRspBuf = 0L;
  370.             (**sendRecord).atpProto.atpRspSize =  0;
  371.             
  372.             (**sendRecord).atpProto.atpUserData = queryStatus;
  373.             
  374.             errCode = ATPRequest(sendRecord,FALSE);
  375.             
  376.             if (errCode==noErr)
  377.             {
  378.                 if ((**sendRecord).atpProto.atpRspUData==idleResponse)
  379.                     idle++;
  380.                 else if ((**sendRecord).atpProto.atpRspUData==busyResponse)
  381.                     busy++;
  382.                 else
  383.                 {
  384.                     sprintf(tempString,"node #%d -- unknown response",slaveNode[i]);
  385.                     DisplayMessage(tempString);
  386.                 }
  387.             }
  388.             else
  389.                 ErrorMessage("ATPRequest",errCode);
  390.         }
  391.     
  392.         DisposHandle(sendRecord);
  393.         
  394.         sprintf(tempString," Busy nodes: %d \r Idle nodes: %d \r Unaccounted nodes: %d",busy,idle,numSlaves-busy-idle);
  395.         CtoPstr(tempString);
  396.         ErrorAlert(tempString);
  397.     }
  398.     
  399.     InitCursor();
  400. }
  401.  
  402.  
  403.  
  404. SendWorkOrder(i,theRow)
  405. int    i,theRow;
  406. {
  407. char            tempString[256];
  408. slaveMessage    theMessage;
  409. ABRecHandle        sendRecord;
  410.  
  411.     sendRecord = (ABRecHandle) NewHandle(atpSize);
  412.     
  413.     theMessage.lims[xmin] = lims[xmin];
  414.     theMessage.lims[xmax] = lims[xmax];
  415.     theMessage.lims[ymin] = lims[ymin];
  416.     theMessage.lims[ymax] = lims[ymax];
  417.     
  418.     theMessage.firstRow = theRow;
  419.     theMessage.lastRow = theRow+pixelSize;
  420.     
  421.     theMessage.maxIterations = maxIterations;
  422.     theMessage.pixelSize = pixelSize;
  423.     theMessage.shading = shading;
  424.     
  425.     sprintf(tempString,"Sending workOrder to node #%d",slaveNode[i]);
  426.     DisplayMessage(tempString);
  427.     
  428.     
  429.     (**sendRecord).atpProto.atpAddress.aNet = slaveNet[i];
  430.     (**sendRecord).atpProto.atpAddress.aNode = slaveNode[i];
  431.     (**sendRecord).atpProto.atpAddress.aSocket = slaveSocket[i];
  432.     
  433.     (**sendRecord).atpProto.atpReqCount = sizeof(slaveMessage);
  434.     (**sendRecord).atpProto.atpDataPtr = (Ptr) &theMessage;
  435.     
  436.     (**sendRecord).atpProto.atpXO = FALSE;
  437.     (**sendRecord).atpProto.atpTimeOut = rtInterval;
  438.     (**sendRecord).atpProto.atpRetries = rtCount;
  439.     
  440.     (**sendRecord).atpProto.atpRspBuf = 0L;
  441.     (**sendRecord).atpProto.atpRspSize = 0;
  442.     
  443.     (**sendRecord).atpProto.atpUserData = workOrder;
  444.     
  445.     errCode = ATPRequest(sendRecord,FALSE);
  446.     
  447.     if ((**sendRecord).atpProto.atpRspUData!=confirmOrder)
  448.         errCode = -1;
  449.     
  450.     if (errCode)
  451.     {
  452.         sprintf(tempString,"Node #%d doesn't respond to workOrder",slaveNode[i]);
  453.         DisplayMessage(tempString);
  454.     }
  455.     
  456.     else
  457.     {
  458.         sprintf(tempString,"workOrder confirmed by node #%d",slaveNode[i]);
  459.         DisplayMessage(tempString);
  460.     }
  461.     
  462.     DisposHandle(sendRecord);
  463.     
  464.     return(errCode);
  465. }
  466.  
  467.  
  468.  
  469. QueryNode(i,theResponse)
  470. int        i,*theResponse;
  471. {
  472. char            tempString[256];
  473. ABRecHandle        sendRecord;
  474.  
  475.     sendRecord = (ABRecHandle) NewHandle(atpSize);
  476.     
  477.     sprintf(tempString,"Querying status of node #%d",slaveNode[i]);
  478.     DisplayMessage(tempString);
  479.     
  480.     (**sendRecord).atpProto.atpAddress.aNet = slaveNet[i];
  481.     (**sendRecord).atpProto.atpAddress.aNode = slaveNode[i];
  482.     (**sendRecord).atpProto.atpAddress.aSocket = slaveSocket[i];
  483.     
  484.     (**sendRecord).atpProto.atpReqCount = 0;
  485.     (**sendRecord).atpProto.atpDataPtr = 0L;
  486.     
  487.     (**sendRecord).atpProto.atpXO = FALSE;
  488.     (**sendRecord).atpProto.atpTimeOut = rtInterval;
  489.     (**sendRecord).atpProto.atpRetries = rtCount;
  490.     
  491.     (**sendRecord).atpProto.atpRspBuf = 0L;
  492.     (**sendRecord).atpProto.atpRspSize = 0;
  493.     
  494.     (**sendRecord).atpProto.atpUserData = queryStatus;
  495.     
  496.     errCode = ATPRequest(sendRecord,FALSE);
  497.     
  498.     if (errCode == noErr)
  499.         *theResponse = (**sendRecord).atpProto.atpRspUData;
  500.     else
  501.         ErrorMessage("ATPRequest",errCode);
  502.     
  503.     DisposHandle(sendRecord);
  504.     
  505.     return(errCode);
  506. }
  507.  
  508.  
  509.  
  510.  
  511. GetBitMap(i,theRow)
  512. int        i,theRow;
  513. {
  514. char            tempString[256];
  515. slaveMessage    theMessage;
  516. ABRecHandle        sendRecord;
  517. int                bitmapSize;
  518. BitMap            mandelBits;
  519.  
  520.     sendRecord = (ABRecHandle) NewHandle(atpSize);
  521.     
  522.     theMessage.firstRow = theRow;
  523.     theMessage.lastRow = theRow+pixelSize;
  524.     
  525.     mandelBits.bounds.left = graphRect.left+1;
  526.     mandelBits.bounds.right = graphRect.right-1;
  527.     mandelBits.bounds.top = theRow;
  528.     mandelBits.bounds.bottom = theRow + pixelSize;
  529.     
  530.     mandelBits.rowBytes = (mandelBits.bounds.right - mandelBits.bounds.left + 7)/8;
  531.     if (mandelBits.rowBytes & 1) mandelBits.rowBytes++;
  532.     
  533.     bitmapSize = mandelBits.rowBytes * (mandelBits.bounds.bottom - mandelBits.bounds.top);
  534.     
  535.     mandelBits.baseAddr = NewPtr(bitmapSize);
  536.     
  537.     
  538.     sprintf(tempString,"Sending bitmapRequest to node #%d",slaveNode[i]);
  539.     DisplayMessage(tempString);
  540.     
  541.     (**sendRecord).atpProto.atpAddress.aNet = slaveNet[i];
  542.     (**sendRecord).atpProto.atpAddress.aNode = slaveNode[i];
  543.     (**sendRecord).atpProto.atpAddress.aSocket = slaveSocket[i];
  544.     
  545.     (**sendRecord).atpProto.atpReqCount = sizeof(slaveMessage);
  546.     (**sendRecord).atpProto.atpDataPtr = (Ptr) &theMessage;
  547.     
  548.     (**sendRecord).atpProto.atpXO = FALSE;
  549.     (**sendRecord).atpProto.atpTimeOut = rtInterval;
  550.     (**sendRecord).atpProto.atpRetries = rtCount;
  551.     
  552.     (**sendRecord).atpProto.atpRspBuf = mandelBits.baseAddr;
  553.     (**sendRecord).atpProto.atpRspSize = bitmapSize;
  554.     
  555.     (**sendRecord).atpProto.atpUserData = bitmapRequest;
  556.     
  557.     errCode = ATPRequest(sendRecord,FALSE);
  558.     
  559.     if (errCode!=noErr)
  560.     {
  561.         ErrorMessage("ATPRequest",errCode);
  562.     }
  563.     
  564.     else 
  565.     {
  566.         if ((**sendRecord).atpProto.atpRspUData==bitmapResponse)
  567.         {
  568.             sprintf(tempString,"bitmapRequest confirmed by node #%d",slaveNode[i]);
  569.             DisplayMessage(tempString);
  570.             
  571.             CopyBits(&mandelBits,&(theWPtr->portBits),&(mandelBits.bounds),&(mandelBits.bounds),srcCopy,0L);
  572.             
  573.             CopyBits(&mandelBits,&graphBits,&(mandelBits.bounds),&(mandelBits.bounds),srcCopy,0L);
  574.         }
  575.         
  576.         else
  577.         {
  578.             sprintf(tempString,"Bad response from node #%d",slaveNode[i]);
  579.             DisplayMessage(tempString);
  580.             
  581.             errCode = -1;
  582.         }
  583.     }
  584.     
  585.     DisposHandle(sendRecord);
  586.     DisposPtr(mandelBits.baseAddr);
  587.     
  588.     return(errCode);
  589. }
  590.